package com.pro01.backend.mapper;

import com.pro01.backend.pojo.TorrentDownload;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface TorrentDownloadMapper {

    //插入下载记录
    @Insert("INSERT INTO torrent_downloads (user_id, torrent_id, peer_id, downloaded_at) " +
            "VALUES (#{userId}, #{torrentId}, #{peerId}, NOW())")
    int insertDownload(TorrentDownload download);

    //判断是否重复下载
    @Select("SELECT COUNT(*) FROM torrent_downloads WHERE peer_id = #{peerId}")
    boolean existsByPeerId(@Param("peerId") String peerId);

    //通过peer_id找到user_id
    @Select("SELECT user_id FROM torrent_downloads WHERE peer_id = #{peerId}")
    Long findUserIdByPeerId(@Param("peerId") String peerId);

    // 查用户下载记录（分页）
//    @Select("SELECT td.torrent_id, t.name, t.category, t.size, td.downloaded_at " +
//            "FROM torrent_downloads td " +
//            "JOIN torrents t ON td.torrent_id = t.id " +
//            "WHERE td.user_id = #{userId} " +
//            "ORDER BY td.downloaded_at DESC " +
//            "LIMIT #{offset}, #{pageSize}")
//    List<Map<String, Object>> getUserDownloadedTorrents(@Param("userId") Long userId,
//                                                        @Param("offset") int offset,
//                                                        @Param("pageSize") int pageSize);
    @Select("""
    SELECT td.id, td.user_id AS userId, td.torrent_id AS torrentId,
           td.peer_id AS peerId, td.downloaded_at AS downloadedAt,
           t.name, t.category, t.size
    FROM torrent_downloads td
    JOIN torrents t ON td.torrent_id = t.id
    WHERE td.user_id = #{userId}
    ORDER BY td.downloaded_at DESC
    LIMIT #{offset}, #{pageSize}
    """)
    List<TorrentDownload> getUserDownloadedTorrents(@Param("userId") Long userId,
                                                    @Param("offset") int offset,
                                                    @Param("pageSize") int pageSize);

    // 用户下载记录总数
    @Select("SELECT COUNT(*) " +
            "FROM torrent_downloads td " +
            "WHERE td.user_id = #{userId}")
    int countUserDownloadedTorrents(@Param("userId") Long userId);

    //查询用户下载种子数
//    @Select("SELECT COUNT(*) FROM torrent_downloads WHERE user_id = #{userId}")
//    int countUserDownloadedTorrents(@Param("userId") Long userId);

    //查询用户今日下载次数
    @Select("SELECT COUNT(*) FROM torrent_downloads WHERE user_id = #{userId} AND DATE(downloaded_at) = CURDATE()")
    int countTodayDownloadsByUserId(@Param("userId") Long userId);

    // 查询今天下载了多少次
    @Select("SELECT COUNT(*) FROM torrent_downloads WHERE user_id = #{userId} AND DATE(downloaded_at) = CURDATE()")
    int countUserTodayDownloads(@Param("userId") Long userId);

    //查询所有用户下载次数
//    @Select("SELECT u.id AS user_id, u.username, COUNT(td.id) AS download_count " +
//            "FROM users u " +
//            "LEFT JOIN torrent_downloads td ON u.id = td.user_id " +
//            "GROUP BY u.id, u.username")
//    List<Map<String, Object>> countAllUsersDownloadsWithUsername();

// 分页查询所有用户的下载次数
    @Select("SELECT u.id AS user_id, u.username, COUNT(td.id) AS download_count " +
            "FROM users u " +
            "LEFT JOIN torrent_downloads td ON u.id = td.user_id " +
            "GROUP BY u.id, u.username " +
            "ORDER BY download_count DESC " +
            "LIMIT #{limit} OFFSET #{offset}")
    List<Map<String, Object>> countAllUsersDownloadsPaged(@Param("offset") int offset, @Param("limit") int limit);

    // 查询总用户数（如果想要分页）
    @Select("SELECT COUNT(*) FROM users")
    int countUsers();

}
